﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MolyMade.Moly.MailMessager.ImapClient
{
    internal class BaseImap
    {
        public BaseImap(string host,int port = -1,string userName =null,string passWord =null,bool useSsl = false)
        {
            if (host != null) this.Host = host;
            if (userName != null) this.UserName = userName;
            if (passWord != null) this.PassWord = passWord;
            this.Port = port;
            this.UseSsl = useSsl;
        }
        private ImapX.ImapClient _client = new ImapX.ImapClient();
        public string Host { get; set; }
        public int Port { get; set; }
        public bool UseSsl { get; set; }
        public string UserName { get; set; }
        public string PassWord { get; set; }
        public void Connect()
        {
            if(_client.IsConnected) return;
            if (this.Port != -1
                ? _client.Connect(this.Host, this.Port, this.UseSsl)
                : _client.Connect(this.Host, this.UseSsl))
            {
                if (!_client.Login(this.UserName, this.PassWord))
                {
                    throw new Exception("Stmp login failure");
                }
            }
            else
            {
                throw new Exception("Stmp connection failure.");
            }
        }
        public void Disconnect()
        {
            if (_client.IsConnected)
            {
                _client.Disconnect();
            }
        }
        public void DownloadMessages(int count = 10)
        {
            _client.Folders.Inbox.Messages.Download(count:count);
        }

        
    }
}
